package com.apes.auto.car.service;

import com.apes.auto.car.model.CarElectronicCatalogue;
import com.apes.auto.car.model.CarThumbnail;
import com.apes.auto.car.model.CarTypeConfiguration;
import com.apes.auto.car.repository.CarElectronicCatalogueRepository;
import com.apes.auto.car.repository.CarThumbnailRepository;
import com.apes.auto.car.repository.CarTypeConfigurationRepository;
import com.apes.auto.maintain.model.LatestCarModel;
import com.apes.auto.maintain.repository.LatestCarModelRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName
 * @Description TODO
 * @Author pengyb
 * @Date 2019/7/17 9:17
 * @Version 1.0
 **/
@Service("carTypeConfigurationService")
public class CarTypeConfigurationService extends DomainService {
    @Autowired
    private CarTypeConfigurationRepository carTypeConfigurationRepository;

    @Autowired
    private CarThumbnailRepository carThumbnailRepository;

    @Autowired
    private CarPartElectronicCatalogueService carPartService;

    @Autowired
    private CarElectronicCatalogueRepository catalogueRepository;

    /**
     * 保存
     *
     * @param request
     * @return
     */
    public CarTypeConfiguration save(SimpleRequest request) {
        CarTypeConfiguration o = request.getO(CarTypeConfiguration.class);
        o.setLRRQ_SJ(new Date());
        CarTypeConfiguration carTypeConfiguration = carTypeConfigurationRepository.saveAndFlush(o);
        syncDZML(carTypeConfiguration);
        syncLatestCar(carTypeConfiguration);
        return carTypeConfiguration;
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    public CarTypeConfiguration delete(SimpleRequest request) {
        CarTypeConfiguration o = request.getO(CarTypeConfiguration.class);
        o.setYXBJ(false);
        o.setLRRQ_SJ(new Date());
        return carTypeConfigurationRepository.saveAndFlush(o);
    }

    /**
     * 同步最新车型表
     *
     * @param configuration
     */
    public LatestCarModel syncLatestCar(CarTypeConfiguration configuration) {
        LatestCarModel latestCarModel = new LatestCarModel();
        latestCarModel.setLevelID(configuration.getLevelID());
        latestCarModel.setBSQMS(configuration.getBSQMS());
        latestCarModel.setBSQLX(configuration.getBSXLB());
//        latestCarModel.setBSXXH(configuration.getBS);
        latestCarModel.setBSQMS(configuration.getBSQMS());
        latestCarModel.setCP(configuration.getVehicleLabel());
        latestCarModel.setCS(configuration.getManufacturers());
        latestCarModel.setCX(configuration.getVehicleType());
        latestCarModel.setCXID(configuration.getPZCXID());
        latestCarModel.setDWSL(configuration.getDWSL());
        latestCarModel.setKT(configuration.getKT());
        latestCarModel.setNK(configuration.getNK());
        latestCarModel.setPL(configuration.getFDJPL());
        latestCarModel.setPP(configuration.getCarBrand());
        latestCarModel.setQCDJ(configuration.getCarLevel());
        latestCarModel.setQCLX(configuration.getCarType());
        latestCarModel.setRLLX(configuration.getRLLX());
        latestCarModel.setXSZDJ(configuration.getXSZDJ());
        latestCarModel.setYJFS(configuration.getYJFS());
        LatestCarModel carModel = SpringManager.getBean(LatestCarModelRepository.class).save(latestCarModel);
        return latestCarModel;

    }


    /**
     * 同步电子目录
     *
     * @param configuration
     */
    public void syncDZML(CarTypeConfiguration configuration) {

        //同步电子目录
        CarElectronicCatalogue catalogue = catalogueRepository.findOne(JpaDsl.toCriteriaByEq("PZCXID", configuration.getPZCXID())).orElse(null);

        if (catalogue != null) {
            throw new RuntimeException("标注为新增的车型" + configuration.getPZCXID() + "已存在电子目录！数据有误，请核对");
        }
        HashMap map = new HashMap();
        map.put("PZCXID", configuration.getPZCXID());
        map.put("OEID", configuration.getCXPJID());
        map.put("LRR", configuration.getLRR().getName());
        map.put("FDJQTJS", configuration.getFDJQTJS());
        carPartService.batchSave(map);

        //将本次新增电子目录的车型ID增加进检索表
        CarElectronicCatalogue entity = new CarElectronicCatalogue();
        entity.setCXPJID(configuration.getCXPJID());
        entity.setPZCXID(configuration.getPZCXID());
        catalogueRepository.saveAndFlush(entity);
    }

    /**
     * 同步车型缩略表
     *
     * @param configuration
     * @param carTypeConfiguration
     * @return
     */
    public void syncCarThumbnail(CarTypeConfiguration configuration, Map carTypeConfiguration) {
        /**
         * 车型缩略表  新增与修订部分 =》直接根据 所谓的去重条件 查询缩略表 有无都直接刷新上去
         */
        CarThumbnail carThumbnail;
        try {
            carThumbnail = carThumbnailRepository.findOne(JpaDsl.toCriteriaByEq("carBrand.id", configuration.getCarBrand().getId(), "carSeries.id", configuration.getCarSeries().getId(),
                    "levelCarType", carTypeConfiguration.get("levelCarType"), "XSBB", configuration.getXSBB(), "CHXXSMC", configuration.getCHXXSMC(), "NK", configuration.getNK(), "emissionStandard.id", configuration.getAutoEmissionStandard() == null ? "" : configuration.getAutoEmissionStandard().getId(), "carType.id", configuration.getCarType().getId(), "carLevel.id", configuration.getCarLevel().getId(),
                    "XSZDJ", configuration.getXSZDJ() == null ? "" : configuration.getXSZDJ(), "YJFS", configuration.getYJFS(), "LEVELFDJXH", carTypeConfiguration.get("LEVELFDJXH"), "FDJXH", configuration.getFDJXH(), "QGRJ", configuration.getQGRJ() == null ? "" : configuration.getQGRJ(),
                    "FDJPL", configuration.getFDJPL() == null ? "" : configuration.getFDJPL(), "JQXS", configuration.getJQXS(), "RLBH", configuration.getRLBH(), "RLLX", configuration.getRLLX(), "ZDGL", configuration.getZDGL(), "ZDML", carTypeConfiguration.get("ZDML"),
                    "QGPLXS", configuration.getQGPLXS(), "QGSL", configuration.getQGSL(), "MQGQMSL", configuration.getMQGQMSL(), "BSXLB", configuration.getBSXLB(), "BSQMS", configuration.getBSQMS(), "LEVELDWSL", carTypeConfiguration.get("LEVELDWSL"),
                    "QZDQLX", carTypeConfiguration.get("QZDQLX") == null ? "" : carTypeConfiguration.get("QZDQLX"), "HZDQLX", carTypeConfiguration.get("HZDQLX") == null ? "" : carTypeConfiguration.get("HZDQLX"), "ZLLX", configuration.getZLLX(), "FDJWZ", configuration.getFDJWZ(), "QDFS", configuration.getQDFS(), "CSXS", configuration.getCSXS(), "ZHOUJ", configuration.getZHOUJ() == null ? "" : configuration.getZHOUJ(),
                    "CMS", configuration.getCMS(), "ZWSL", configuration.getZWSL(), "QLTGG", carTypeConfiguration.get("QLTGG") == null ? "" : carTypeConfiguration.get("QLTGG"), "HLTGG", carTypeConfiguration.get("HLTGG") == null ? "" : carTypeConfiguration.get("HLTGG"), "QLGGG", carTypeConfiguration.get("QLGGG") == null ? "" : carTypeConfiguration.get("QLGGG"), "HLGGG", carTypeConfiguration.get("HLGGG") == null ? "" : carTypeConfiguration.get("HLGGG")
            )).orElse(null);
        } catch (Exception e) {
            throw new RuntimeException("取值错" + configuration.getPZCXID() + e);
        }

        try {
            if (configuration.getGGBZ().equals("删除")) {
                if (carThumbnail != null) {
                    List<CarTypeConfiguration> all = carTypeConfigurationRepository.findAll(JpaDsl.toCriteriaByEq("carThumbnail.id", carThumbnail.getId(), "YXBJ", 1));

                    if (all != null && all.size() != 0) {
                        if (all.size() > 1) {
                            for (CarTypeConfiguration car : all) {
                                if (!car.getPZCXID().equals(carThumbnail.getPZCXID())) {
                                    CarThumbnail thumbnail = car.getCarThumbnail();
                                    thumbnail.setPZCXID(car.getPZCXID());
                                    thumbnail.setLevelID(car.getLevelID());
                                    carThumbnail = carThumbnailRepository.saveAndFlush(thumbnail);
                                    break;
                                }
                            }
                        } else {
                            carThumbnailRepository.delete(carThumbnail.getId());
                        }
                    }
                }
            } else {
                if (carThumbnail == null) {
                    carThumbnail = MapUtil.getO(carTypeConfiguration, CarThumbnail.class);
                }
                carThumbnail = carThumbnailRepository.saveAndFlush(carThumbnail);

            }

        } catch (Exception e) {
            throw new RuntimeException("逻辑错" + configuration.getPZCXID());
        }

        if (carThumbnail != null) {
            configuration.setCarThumbnail(carThumbnail);
        }
    }


}
